package Petgoods;

import java.sql.*;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author ryl
 * @version 1.0
 * @data 2025/4/2 11:21
 */
public class JdbcUtil {
    // 数据库连接信息
    private static final String url = "jdbc:mysql://192.168.17.130:3306/petgoos";
    private static final String user = "root";
    private static final String password = "123456";
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    // 初始连接数
    private static final int INITIAL_POOL_SIZE = 10;
    // 最大连接数
    private static final int MAX_POOL_SIZE = 20;
    // 定义连接池
    private static final LinkedList<Connection> connectionPool = new LinkedList<>();
    // 定义锁
    private static final ReentrantLock lock = new ReentrantLock();

    {
        try {
            // 加载数据库驱动程序类
            Class.forName(DRIVER);
            // 初始化连接池
            for (int i = 0; i < INITIAL_POOL_SIZE; i++) {
                connectionPool.add(creatConnection());
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 创建数据库连接 returen 连接对象
    private Connection creatConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    /**
     * 连接归还给池子
     *
     * @param connection
     */
    public void returnConnection(Connection connection) {
        lock.lock();
        try {
            if (connection != null) {
                connectionPool.addLast(connection);
            }
        } finally {
            lock.unlock();
        }
    }

    /**
     * 从连接池中获取链接
     *
     * @return 返回一个可用连接
     * @throws SQLException
     */
    public Connection getConn() throws SQLException {
        lock.lock();
        try {
            if (connectionPool.isEmpty()) {
                if (connectionPool.size() < MAX_POOL_SIZE) {
                    // 如果连接池里为空且没有达到最大限制,创建新链接
                    return creatConnection();
                } else {
                    throw new SQLException("连接池已满");
                }
            }
            return connectionPool.removeFirst();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 关闭所有连接,并清空连接池子
     */
    public void closeAllConnection() {
        lock.lock();
        try {
            for (Connection connection : connectionPool) {
                if (connection != null && !connection.isClosed()) { // 连接不为空，连接也没关
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                connectionPool.clear();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 关闭数据库和资源
     *
     * @param rs
     * @param conn
     * @param ps
     */
    public void closeResource(ResultSet rs, Connection conn, PreparedStatement ps) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}